/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.ohos.timber;

import ohos.hiviewdfx.HiLog;

import io.sentry.Breadcrumb;
import io.sentry.IHub;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.protocol.Message;
import timber.log.Timber;

/**
 * SentryTimberTree
 *
 * @since 2021-05-26
 */
public class SentryTimberTree extends Timber.Tree {
    private IHub hub;
    private SentryLevel minEventLevel;
    private SentryLevel minBreadcrumbLevel;

    public SentryTimberTree(IHub hub, SentryLevel minEventLevel, SentryLevel minBreadcrumbLevel) {
        this.hub = hub;
        this.minEventLevel = minEventLevel;
        this.minBreadcrumbLevel = minBreadcrumbLevel;
    }

    private Boolean isLoggable(SentryLevel level, SentryLevel minLevel) {
        return level.ordinal() >= minLevel.ordinal();
    }

    @Override
    protected void log(int priority, String tag, String message, Throwable throwable) {
        SentryLevel level = getSentryLevel(priority);
        captureEvent(level, tag, message, throwable);
        addBreadcrumb(level, message);
    }

    private void captureEvent(SentryLevel sentryLevel, String tag, String msg, Throwable throwable) {
        if (isLoggable(sentryLevel, minEventLevel)) {
            SentryEvent sentryEvent = new SentryEvent();
            sentryEvent.setLevel(sentryLevel);
            if (throwable != null) {
                sentryEvent.setThrowable(throwable);
            }

            Message message = new Message();
            message.setFormatted(msg);
            sentryEvent.setMessage(message);

            if (tag != null) {
                sentryEvent.setTag("TimberTag", tag);
            }

            sentryEvent.setLogger("Timber");

            hub.captureEvent(sentryEvent);
        }
    }

    private void addBreadcrumb(SentryLevel sentryLevel, String msg) {
        if (isLoggable(sentryLevel, minBreadcrumbLevel)) {
            Breadcrumb breadcrumb = new Breadcrumb();
            breadcrumb.setLevel(sentryLevel);
            breadcrumb.setCategory("Timber");
            breadcrumb.setMessage(msg);
            hub.addBreadcrumb(breadcrumb);
        }
    }

    private SentryLevel getSentryLevel(int priority) {
        SentryLevel level = SentryLevel.DEBUG;
        switch (priority) {
            case HiLog.FATAL:
                level = SentryLevel.FATAL;
                break;
            case HiLog.ERROR:
                level = SentryLevel.ERROR;
                break;
            case HiLog.WARN:
                level = SentryLevel.WARNING;
                break;
            case HiLog.INFO:
                level = SentryLevel.INFO;
                break;
            case HiLog.DEBUG:
            case HiLog.LOG_APP:
                level = SentryLevel.DEBUG;
                break;
        }
        return level;
    }
}
